package common.log.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Service;

import common.hibernate.service.impl.BaseServiceImpl;
import common.log.service.LogService;
import common.log.vo.ApplicationLog;
import common.log.vo.WebLog;
import common.rbac.po.Log;
import common.security.event.LogoutEvent;
import common.security.web.util.SecurityUtil;
import common.util.UUIDUtil;

@Service
public class LogServiceImpl extends BaseServiceImpl implements LogService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * common.security.service.impl.LogServcie#saveLog(org.springframework.context
	 * .ApplicationEvent, java.lang.Integer, common.security.web.util.LogType,
	 * boolean, java.lang.String)
	 */
	@Override
	public void saveLog(ApplicationLog alog) {
		String userId = "Anonymous";
		String ip = "0.0.0.0";
		Authentication authentication = null;
		ApplicationEvent event = alog.getEvent();
		if (event instanceof AbstractAuthenticationEvent) {
			authentication = ((AbstractAuthenticationEvent) event).getAuthentication();
		} else if (event instanceof LogoutEvent) {
			authentication = (Authentication) event.getSource();
		}
		WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
		userId = authentication.getName();
		ip = details.getRemoteAddress();
		Log log = new Log();
		log.setId(UUIDUtil.createUuid());
		this.setModule(alog.getModule(), log);
		log.setType(alog.getType().getValue());
		log.setTime(new Date());
		log.setUserId(userId);
		log.setIp(ip);
		log.setMsg(alog.getMsg());
		log.setIsSuccess(alog.isSuccess());

		this.save(log);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see common.security.service.impl.LogServcie#saveLog(java.lang.Integer,
	 * common.security.web.util.LogType, boolean, java.lang.String)
	 */
	@Override
	public void saveLog(WebLog vo) {
		Log log = new Log();
		log.setId(UUIDUtil.createUuid());
		this.setModule(vo.getModule(), log);
		log.setType(vo.getType().getValue());
		log.setTime(new Date());
		log.setUserId(SecurityUtil.getUserName());
		log.setIp(SecurityUtil.getUserIp());
		log.setIsSuccess(vo.isSuccess());
		log.setMsg(vo.getMsg());
		this.save(log);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * common.security.service.impl.LogServcie#saveLogForNoUser(java.lang.Integer
	 * , common.security.web.util.LogType, boolean, java.lang.String)
	 */
	@Override
	public void saveLogForNoUser(WebLog vo) {
		String userId = "Anonymous";
		String ip = "0.0.0.0";
		Log log = new Log();
		log.setId(UUIDUtil.createUuid());
		this.setModule(vo.getModule(), log);
		log.setType(vo.getType().getValue());
		log.setTime(new Date());
		log.setUserId(userId);
		log.setIp(ip);
		log.setIsSuccess(vo.isSuccess());
		log.setMsg(vo.getMsg());
		this.save(log);
	}

	/**
	 * 自动填写大小模块的编码
	 * 
	 * @param module
	 * @param log
	 */
	private void setModule(Integer module, Log log) {
		log.setBigModule(Integer.valueOf(StringUtils.substring(module.toString(), 0, 2)));
		log.setLittleModule(module);
	}

}
